<template>
  <div class="home">
    <el-container>
      <!-- 顶栏 -->
      <el-header>
        <div
          class="header-title"
          style="cursor: pointer"
          @click="$router.push('/home')"
        >
          后台管理系统
        </div>
        <div class="signout" @click="signOut">
          <div class="header-img"><img src="@/assets/head.jpg" alt="" /></div>
          <span>退出登录</span>
        </div>
      </el-header>
      <!-- 内容区 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 展开收起按钮 -->
          <div class="asideopen" @click="isflag">
            <i class="el-icon-menu"></i>
          </div>

          <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
          >
            <el-submenu
              v-for="val in menulist"
              :key="val.id"
              :index="val.id + ''"
            >
              <!-- 一级菜单模板区 -->
              <template slot="title">
                <i :class="iconlist[val.id]"></i>
                <span>{{ val.authName }}</span>
              </template>
              <!-- 二级菜单模板区 -->
              <el-menu-item
                v-for="item in val.children"
                :key="item.id"
                :index="item.path"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ item.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体内容 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { menus } from "@/api/index.js";
export default {
  data() {
    return {
      isCollapse: false,
      menulist: [],
      iconlist: {
        125: "el-icon-user-solid",
        103: "el-icon-s-tools",
        101: "el-icon-s-goods",
        102: "el-icon-s-order",
        145: "el-icon-s-data",
      },
    };
  },
  methods: {
    signOut() {
      //退出登录，移除会话缓存中的token并返回登录页
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    //展开收起点击事件
    isflag() {
      this.isCollapse = !this.isCollapse;
    },
  },
  created() {
    menus().then((res) => {
      this.menulist = res.data.data;
    });
  },
};
</script>

<style lang="less" scoped>
.home {
  width: 100%;
  height: 100%;

  .el-container {
    width: 100%;
    height: 100%;

    .el-header {
      background-color: #606266;
      color: white;
      display: flex;
      justify-content: space-between;

      .header-title {
        height: 100%;
        font-size: 24px;
        line-height: 60px;
      }

      .signout {
        height: 100%;
        width: 50px;
        font-size: 12px;
        float: right;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        cursor: pointer;

        .header-img {
          width: 40px;
          height: 40px;
          border-radius: 20px;
          overflow: hidden;

          img {
            width: 40px;
          }
        }
      }
    }

    .el-aside {
      background-color: #545c64;
      overflow: auto;

      .el-menu {
        border: 0;
      }

      .asideopen {
        width: 100%;
        height: 40px;
        font-size: 20px;
        color: white;
        text-align: center;
        line-height: 40px;
        cursor: pointer;
      }

      .asideopen:hover {
        background-color: rgb(67, 74, 80);
        color: #ffd04b;
      }
    }
    .el-main {
      width: 100%;
      height: 100%;
      padding: 0;
    }
  }
}
</style>
